Programming in C by Munishwar Gulati & Mini Gulati

Programming in C by Munishwar Gulati & Mini Gulati

Author:Munishwar Gulati & Mini Gulati [Gulati, Munishwar]
Language: eng
Format: epub
Published: 2016-11-30T08:00:00+00:00


POINTERS

POINTERS AND ARRAYS

DYNAMIC MEMORY ALLOCATION

STRING POINTER

POINTERS AND FUNCTIONS

POINTER TO A FUNCTION

140

Chapter 6 : Pointers

POINTERS

The variables are stored in memory at specific locations. These

location have the addresses. When the name of the variable is used,

it translate the name into the addresses. The procedure is automatic

and need no concern of the programmer.

The memory addresses can be used because of the fact that memory

addresses are global to all functions, whereas local variable names

are meaningful only within the functions in which they are declared.

A function can pass the address of local variable to another functions,

and the second function can use this address to access the contents

of the first function’s local variables.

The computer’s memory is a sequential collection of storage cells.

Each cell, commonly known as byte has a number called address

associated with it. Typically, the addresses are numbered

consecutively, starting from zero. The last address depends on the

memory size. A computer system having 64K memory will have its

last address as 65,535.

Whenever a variable is declared, the system allocates it somewhere in

the memory at an appropriate location to hold the value of the

variable. Since every byte has a unique address number, this

location will also have its own address number.

Consider the following variable

int addr = 15;

This statement will store the variable addr at some location and will

but value 15 is that location. Let us assume the address of the

location as 2001.

During execution of the program, the system always associates the

name addr with the address 2001. The value 15 can be accessed by

using either the variable name addr or the address 2001.

Since memory address are simply numbers, they can be assigned to

some variables which can be stored in memory, like any other

variable. Such variable that hold memory addresses are called

pointers. A pointer is, therefore, nothing but a variable that contains

an address which is a location of another variable in memory.

It must be noted down, that since a pointer is a variable, its value is

also stored in the memory in another variable in memory at some

other location. Let us assign the address of quantity to a variable ad.

The links between variable ad and addr can be shown as

ad

2001

15

addr

Chapter 6 : Pointers

141

Since the value of ad is address of the variable addr, therefore, it may

be said that the variable ad points to the variable addr. Thus ad gets

the name pointer.

THE ADDRESS OPERATOR

The actual location of a variable, where it is stored, called address of

the variable, is system dependent. Addresses are a sperate data type

in C, distinct from other types introduced so far. The address of a

variable is not known to us immediately.

The ampersand (&) immediately preceding a variable name in a call

to the scanf function returns the address of the variable associated

with it. The operator is known as address operator and it is not

restricted to scanf function. It can be used with any variable name or

array element, but not the constant, to know the address of variable

ar array element respectively.

e.g.

p = &addr;

The above statement would result in p = 2001 as addr has been stored

at the address number 2001.

e.g.

main ()

{

char grade;

int



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.